package engine.logic
{
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.ui.AlertUI;
	
	import flash.events.IEventDispatcher;
	import flash.external.ExternalInterface;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	
	/**
	 * 用于处理特定的自定义消息所引发的逻辑。 
	 * @author wangjun
	 * 
	 */	
	public class CustomLogic extends BaseObject
	{
		private static		var _self				:CustomLogic;
		private 			var model				:ModelLocator;
		
		public function CustomLogic(target:IEventDispatcher=null)
		{
			super(target);
			init()
		}
		
		public static function getInstance():CustomLogic
		{
			if ( _self == null )
			{
				_self = new CustomLogic();
			}
			return _self;
		}
		
		private function init():void
		{
			model		=	ModelLocator.getInstance();	
			
			//侦听打开收藏夹请求消息
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_OPEN_COLLECTIONUI),
				openFavorite);		
		}
		
		/**
		 * 打开收藏夹并创建桌面快捷方式e:CustomMsgEvent
		 * */
		public function openFavorite():void
		{
//			var address:String	=	"";
//			if(ExternalInterface.available)
//			{
//				address	=ExternalInterface.call("window.location.href.toString");
//			}
			var tStr:String	=	<![CDATA[
				function func(url,title)
				{
					if(document.all)
					{
						try
						{
							window.external.addFavorite(url,title);
						}
						catch (e1)
						{
							try
							{
								window.external.addToFavoritesBar(url,title);
							}
							catch (e2)
							{
								return true;
							}
						}
					}
					else if(window.sidebar)
					{
						window.sidebar.addPanel(title,url,"");
					}
					else
					{
						return true;
					}
					return false;
				}
				func("]]>+model.serverIni.getContent("gameFavorite","gameweburl")+
				'","'+model.serverIni.getContent("gameFavorite","gamewebtitle")+'");';
			
			var dStr:String	=	<![CDATA[
				function func(url,title)
				{
					try
					{
						var fso = new ActiveXObject("Scripting.FileSystemObject");
						var WshShell = new ActiveXObject("WScript.Shell");
						var tagFolder = WshShell.SpecialFolders("Desktop");
						if(!fso.FolderExists(tagFolder)){
							fso.CreateFolder(tagFolder);
						}
						if(!fso.FileExists(tagFolder + "//" + title + ".lnk")){
							var link = WshShell.CreateShortcut(tagFolder + "//" + title + ".lnk");
							link.TargetPath = url;
							link.WindowStyle = 3;
							link.Save();
						}
						
					}
					catch(e)
					{
						
					}
				}
				func("]]>+model.mainIni.getContent("gameFavorite","gameweburl")+
				'","'+model.mainIni.getContent("gameFavorite","gamewebtitle")+'");';
			
			if(ExternalInterface.available)
			{
				var isNot:Boolean	=	ExternalInterface.call("eval", tStr);
				var _isNot:Boolean	=	ExternalInterface.call("eval", dStr);
				if(isNot)
				{
					AlertUI.showAlert(
						StaticIni.getSwfLangStr("MessageUI_Notice"),
						StaticIni.getSwfLangStr("UIStr_1001122"));
				}
			}
		}
	}
}